/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年4月10日         luwei
 */

package com.tianci.appstore.ui.list.bootsetting;

import android.content.Context;
import android.graphics.Color;
import android.os.RemoteException;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.skyworth.framework.skysdk.properties.SkyGeneralProperties;
import com.tianci.appstore.R;
import com.tianci.appstore.audio.IAudio;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.list.ListBaseView;
import com.tianci.appstore.ui.list.bootsetting.AppSelectedData.AppItem;
import com.tianci.appstore.utils.SkyConstants;

public class LauncherOnBootLayout extends ListBaseView {

    private Context mContext = null;
    private LauncherItem mLauncherItem = null;
    private LaunchOnBootListView mOnBootListView = null;
    public static SkyListAppData mStartApp = new SkyListAppData("", "", "", "", "");
    private boolean hideStartupApp = false;

    public LauncherOnBootLayout(Context context) {
        super(context);
        this.mContext = context;
        //hideStartupApp = SkyGeneralProperties.getBoolProperty("NEW_BOOT_FLOW");
        if (!hideStartupApp){
            getStartUpApp();
            addLauncherTitle();
            addSelectedItem();
        }
        addLauncherOptimization();
        addOptimizationLayout();
    }

    private void getStartUpApp() {
        try {
            String result = LiteContentServiceConnecter.getService().call(
                    "setting://com.coocaa.app/startup_app?action=get");
            if (result != null) {
                AppSelectedData data = JSONObject.parseObject(result, AppSelectedData.class);
                if (data.error_code == 0 && data.resource != null && data.resource.total_count > 0) {
                    AppItem item = data.resource.resource_items.get(0);
                    if (Android.isPackageInstalled(mContext, item.pkgname)) {
                        mStartApp = new SkyListAppData("", item.label, "", "", item.pkgname);
                        mStartApp.setMainActivity(item.activity);
                    } else {
                        LiteContentServiceConnecter.getService().call(
                                "setting://com.coocaa.app/startup_app?action=set&pkgname=" + ""
                                        + "&activity=" + "");
                        mStartApp = new SkyListAppData("", "", "", "", "");
                        mStartApp.setMainActivity("");
                    }
                } else {
                    mStartApp = new SkyListAppData("", "", "", "", "");
                    mStartApp.setMainActivity("");
                }
            }
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void addLauncherTitle() {
        TextView textView = new TextView(mContext);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(SkyConstants.Dpi(35));
        String tips = getResources().getString(R.string.launch_on_boot_setting_tips);
        SpannableString s = new SpannableString(tips);
        int start = tips.indexOf("（");
        s.setSpan(new RelativeSizeSpan(0.8f), start, tips.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(s);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        params.leftMargin = SkyConstants.Div(115);
        params.topMargin = SkyConstants.Div(98);
        this.addView(textView, params);
    }

    private void addSelectedItem() {
        mLauncherItem = new LauncherItem(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(SkyConstants.Div(700),
                SkyConstants.Div(273));
        params.topMargin = SkyConstants.Div(132);
        params.leftMargin = SkyConstants.Div(87);
        addView(mLauncherItem, params);
        mLauncherItem.requestFocus();
        if (mStartApp != null) {
            mLauncherItem.update(mStartApp);
        }
    }

    private void addLauncherOptimization() {
        TextView textView = new TextView(mContext);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(SkyConstants.Dpi(35));
        String tips = getResources().getString(R.string.launch_on_boot_optimization);
        SpannableString s = new SpannableString(tips);
        int start = tips.indexOf("（");
        s.setSpan(new RelativeSizeSpan(0.8f), start, tips.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(s);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        if (!hideStartupApp){
            params.leftMargin = SkyConstants.Div(115);
            params.topMargin = SkyConstants.Div(420);
        }else{
            params.leftMargin = SkyConstants.Div(120);
            params.topMargin = SkyConstants.Div(120);
        }

        this.addView(textView, params);
    }

    private void addOptimizationLayout() {
        mOnBootListView = new LaunchOnBootListView(mContext);
        mOnBootListView.setStartApp(mStartApp);
        mOnBootListView.setBackgroundColor(android.R.color.transparent);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        addView(mOnBootListView, params);
    }

    @Override
    public void destroyController() {
        if (mOnBootListView != null) {
            mOnBootListView.destroyController();
        }
    }

    @Override
    public void resume() {
        getStartUpApp();
        mLauncherItem.update(mStartApp);
        mOnBootListView.refreshStartApp(mStartApp);
    }

    @Override
    public IAudio getAudioInterface() {
        return mOnBootListView.getAudioInterface();
    }
}
